package com.ruoyi.aliai.config;


import com.alibaba.cloud.ai.dashscope.api.DashScopeApi;
import com.alibaba.cloud.ai.dashscope.embedding.DashScopeEmbeddingModel;
import com.alibaba.cloud.ai.dashscope.embedding.DashScopeEmbeddingOptions;
import org.springframework.ai.document.MetadataMode;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.embedding.TokenCountBatchingStrategy;
import org.springframework.ai.vectorstore.redis.RedisVectorStore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPooled;

import java.time.LocalDate;
import java.util.function.Function;


@Configuration
public class AIConfig {

	//配置向量数据库  手动配置的方式  TODO 后续更改为redis 或者mongDB  待删除
	@Bean(name = "customVectorStore") // 重命名Bean
	RedisVectorStore vectorStore(JedisPooled jedisPooled, EmbeddingModel embeddingModel) {
		return RedisVectorStore.builder(jedisPooled, embeddingModel)
				.indexName("default-index")                //todo 暂时没有找到修改标签的方法
				.prefix("medi-prefix")                  // Optional: defaults to "embedding:"
				.metadataFields(                         // Optional: define metadata fields for filtering
						RedisVectorStore.MetadataField.tag("country"),
						RedisVectorStore.MetadataField.numeric("year"))
				.initializeSchema(true)                   // Optional: defaults to false
				.batchingStrategy(new TokenCountBatchingStrategy()) // Optional: defaults to TokenCountBatchingStrategy
				.build();
	}

	@Bean
	public JedisPooled jedisPooled() {
		return new JedisPooled("redis-11793.crce178.ap-east-1-1.ec2.redns.redis-cloud.com", 11793,"default", "8bcXlfvzPC7IVmK9SCIGG1eLyP0jmNTz");
	}

	//配置，模型上下文
	@Bean
	public EmbeddingModel embeddingModel() {
		//
		var dashScopeApi = new DashScopeApi(System.getenv("AI_DASHSCOPE_API_KEY"));
		return new DashScopeEmbeddingModel(dashScopeApi, MetadataMode.EMBED,
				DashScopeEmbeddingOptions.builder()
						.withModel("text-embedding-v2")
						.build());
	}

}
